package com.pigs.pigsordersystem.controller;


import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pigs.pigsordersystem.entity.*;
import com.pigs.pigsordersystem.mapper.InventoryMapper;
import com.pigs.pigsordersystem.mapper.UsersMapper;
import com.pigs.pigsordersystem.service.IFinanceService;
import com.pigs.pigsordersystem.service.IInventoryService;
import com.pigs.pigsordersystem.service.IProductService;
import com.pigs.pigsordersystem.service.IUsersService;
import com.pigs.pigsordersystem.util.ResultPagingUtil;
import com.pigs.pigsordersystem.util.ResultUtil;
import com.pigs.pigsordersystem.util.TimeUtitl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Y.yang
 * @since 2020-03-14
 */
@RestController
@RequestMapping("/finance")
@Api(value = "/finance", description = "财务表控制器")
public class FinanceController {

    @Autowired
    private IFinanceService financeService;

    @Autowired
    private FinanceEntity financeEntity;

    @Autowired
    private UsersMapper usersMapper;

    private AbstractWrapper queryWrapper = new QueryWrapper<UsersEntity>();

    private AbstractWrapper inventoryQueryWrapper = new QueryWrapper<InventoryEntity>();

    @Autowired
    private IInventoryService iInventoryService;

    @Autowired
    private InventoryMapper inventoryMapper;

    @Autowired
    private IProductService productService;

    private Logger logger = LoggerFactory.getLogger(FinanceController.class);

    /**
     * 购买商品
     * buyProducts
     */
    @PostMapping("/buyProducts")
    @ApiOperation(value = "购买商品")
    public ResultFormat buyProducts(@RequestParam("productsId") Integer productsId, @RequestParam("aggregatePrice") Double aggregatePrice, @RequestParam("productNum") Integer productNum, HttpSession session) {

        String userName = (String) session.getAttribute("userName");
        System.out.println(userName);
        System.out.println(productsId);
        System.out.println(aggregatePrice);
        System.out.println(productNum);
        /**
         * 判断session是否有值
         * 如果有就查询客户的信息
         * 获取客户 id
         * 添加 账户表 属性
         */
        if (!userName.isEmpty()) {
            queryWrapper.eq("name", userName);
            UsersEntity usersEntity = usersMapper.selectOne(queryWrapper);
            System.out.println(usersEntity);
            financeEntity.setProductId(productsId);
            financeEntity.setAggregatePrice(aggregatePrice);
            financeEntity.setUsersId(usersEntity.getId());
            financeEntity.setProductNum(productNum);

            boolean save = financeService.save(financeEntity);

            inventoryQueryWrapper.eq("product_id", productsId);

            InventoryEntity inventoryEntity = inventoryMapper.selectOne(inventoryQueryWrapper);

            System.out.println(inventoryEntity);

            Integer flag = iInventoryService.updateInventoryNum(inventoryEntity.getProductId(),productNum, TimeUtitl.dateTime());

            System.out.println(flag);

            System.out.println(save);
            if (save) {
                return ResultUtil.success(save);
            }
        }
        return ResultUtil.error(100, "购买失败");
    }


    /**
     *查询客户订单购买记录
     *
     * @return
     */
    @GetMapping("/queryOrderList")
    @ApiOperation(value = "查询客户订单购买记录")
    public ResultFormatPaging queryOrderList(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit,HttpSession session) {

        Page<ProductEntity> iPage = new Page<ProductEntity>(page,limit);

        String userName = (String) session.getAttribute("userName");
        IPage<ProductEntity> moveBookingEntityIPage = productService.queryOrderList(iPage, userName);

        List<ProductEntity> productEntities = iPage.getRecords();
        logger.info("productEntities={}", productEntities);

        return ResultPagingUtil.pagingSuccess(0, (int) iPage.getTotal(), productEntities);
    }

}
